package com.tao.aszx.manager.service.Impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tao.aszx.manager.mapper.*;
import com.tao.aszx.manager.service.SysMenuService;
import com.tao.aszx.manager.service.SysRoleService;
import com.tao.aszx.model.dto.system.ParcelsDto;
import com.tao.aszx.model.dto.system.SysRole;
import com.tao.aszx.model.dto.system.SysRoleDto;
import com.tao.aszx.model.entity.system.Couriers;
import com.tao.aszx.model.entity.system.RecipientAddress;
import com.tao.aszx.model.entity.system.SenderAddress;
import com.tao.aszx.model.entity.system.SysMenu;
import com.tao.aszx.model.vo.system.ParcelsVo;
import io.swagger.v3.core.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;
    @Autowired
    private SenderAddressMapper senderAddressMapper;
    @Autowired
    private RecipientAddressMapper recipientAddressMapper;

    @Autowired
    private CouriersMapper couriersMapper;


    @Autowired
    private SysMenuService sysMenuService;
    @Override
    public PageInfo<ParcelsVo> findByPage(SysRoleDto sysRoleDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum , pageSize) ;
        List<ParcelsVo> sysRoleList = sysRoleMapper.findByPage(sysRoleDto) ;

        sysRoleList.forEach(item->{
            item.setSenderAddr(item.getSenderAddress().getProvince()+
                    item.getSenderAddress().getCity()+
                    item.getSenderAddress().getDistrict()+
                    item.getSenderAddress().getStreetAddress());
            item.setRecipientAddr(item.getRecipientAddress().getProvince()+
                    item.getRecipientAddress().getCity()+
                    item.getRecipientAddress().getDistrict()+
                    item.getRecipientAddress().getStreetAddress());
        });
        PageInfo<ParcelsVo> pageInfo = new PageInfo(sysRoleList) ;
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int saveSysRole(ParcelsDto parcelsDto) {

        SenderAddress senderAddress = parcelsDto.getSenderAddress();
        List<String> senderAddress1 = senderAddress.getAddress();
        senderAddress.setProvince(senderAddress1.get(0));
        senderAddress.setCity(senderAddress1.get(1));
        senderAddress.setDistrict(senderAddress1.get(2));
        senderAddressMapper.save(senderAddress);
        Long senderAddressId = senderAddress.getId();

        RecipientAddress recipientAddress = parcelsDto.getRecipientAddress();
        List<String> recipientAddress1 = recipientAddress.getAddress();
        recipientAddress.setProvince(recipientAddress1.get(0));
        recipientAddress.setCity(recipientAddress1.get(1));
        recipientAddress.setDistrict(recipientAddress1.get(2));
        recipientAddressMapper.save(recipientAddress);
        Long recipientAddressId = recipientAddress.getId();

        //根据姓名查询快递员id
        Couriers couriers = couriersMapper.findByName(parcelsDto.getCouriers().getName());
        Integer courierId = couriers.getCourierId();
        //解析收货人地址
//        List<String> recipientAddress = parcelsDto.getRecipientAddress();
//        RecipientAddress recipientAddress1 = new RecipientAddress();
//        recipientAddress1.setProvince(recipientAddress.get(0));
//        recipientAddress1.setCity(recipientAddress.get(1));
//        recipientAddress1.setDistrict(recipientAddress.get(2));
//        recipientAddress1.setName(parcelsDto.getRecipientName());
//        recipientAddress1.setPhone(parcelsDto.getRecipientPhone());
//        recipientAddress1.setStreetAddress(parcelsDto.getRecipientDetailed());
//        recipientAddressMapper.save(recipientAddress1);
//        Long recipientAddressId = recipientAddress1.getId();


        //解析寄件人信息
//        List<String> senderAddress = parcelsDto.getSenderAddress();
//        SenderAddress senderAddress1 = new SenderAddress();
//        senderAddress1.setProvince(senderAddress.get(0));
//        senderAddress1.setCity(senderAddress.get(1));
//        senderAddress1.setDistrict(senderAddress.get(2));
//        senderAddress1.setName(parcelsDto.getSenderName());
//        senderAddress1.setPhone(parcelsDto.getSenderPhone());
//        senderAddress1.setStreetAddress(parcelsDto.getSenderDetailed());
//        senderAddressMapper.save(senderAddress1);
//
//        Long senderAddressId = recipientAddress1.getId();


        return sysRoleMapper.saveParcels(recipientAddressId , senderAddressId,parcelsDto.getWeight(), courierId.longValue());
    }



    @Override
    @Transactional
    public void updateSysRole(ParcelsDto parcelsDto) {
        //修改收货人
//        parcelsDto.setStatus(JSON.par);
        List<String> address = parcelsDto.getRecipientAddress().getAddress();
        parcelsDto.getRecipientAddress().setProvince(address.get(0));
        parcelsDto.getRecipientAddress().setCity(address.get(1));
        parcelsDto.getRecipientAddress().setDistrict(address.get(2));
        sysRoleMapper.updateRicipientAddressById(parcelsDto.getRecipientAddress());

        //修改寄件人
        List<String> address1 = parcelsDto.getSenderAddress().getAddress();
        parcelsDto.getSenderAddress().setProvince(address1.get(0));
        parcelsDto.getSenderAddress().setCity(address1.get(1));
        parcelsDto.getSenderAddress().setDistrict(address1.get(2));
        sysRoleMapper.updateSenderAddressById(parcelsDto.getSenderAddress());


        //修改订单
        sysRoleMapper.updateParcelsById(parcelsDto);
    }

    @Override
    public void deleteById(Long roleId) {

        sysRoleMapper.deleteById(roleId);
    }

    @Override
    public Map<String, Object> findAllRoles(Long userId) {
        // 查询所有的角色数据
        List<SysRole> sysRoleList = sysRoleMapper.findAllRoles() ;

        // 查询当前登录用户的角色数据
        List<Long> sysRoles = sysRoleUserMapper.findSysUserRoleByUserId(userId);

        // 构建响应结果数据
        Map<String , Object> resultMap = new HashMap<>() ;
        resultMap.put("allRolesList" , sysRoleList) ;
        resultMap.put("sysUserRoles", sysRoles);

        return resultMap;
    }


    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {
        // 查询所有的菜单数据
        List<SysMenu> sysMenuList = sysMenuService.findSysMenus() ;

        // 查询当前角色的菜单数据
        List<Long> roleMenuIds = sysRoleMapper.findSysRoleMenuByRoleId(roleId) ;

        // 将数据存储到Map中进行返回
        Map<String , Object> result = new HashMap<>() ;
        result.put("sysMenuList" , sysMenuList) ;
        result.put("roleMenuIds" , roleMenuIds) ;

        // 返回
        return result;
    }
}
